package com.lxyk.yygh.order.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxyk.yygh.common.result.Result;
import com.lxyk.yygh.common.utils.AuthContextHolder;
import com.lxyk.yygh.enums.OrderStatusEnum;
import com.lxyk.yygh.model.order.OrderInfo;
import com.lxyk.yygh.order.service.OrderService;
import com.lxyk.yygh.vo.order.OrderCountQueryVo;
import com.lxyk.yygh.vo.order.OrderQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {
    @Autowired
    private OrderService orderService;

    /**
     * @ApiParam
     * value:入参的名称，就是前端传给后端的名字
     * name:对参数名的描述，就是入参的中文意思
     * required:给入参添加限制条件，false说明前端可传可不传，true则必传，不传报错
     * @return
     */
    @ApiOperation(value = "创建订单")
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public Result submitOrder(@ApiParam(name = "scheduleId",value = "排班id",required = true)
                              @PathVariable String scheduleId,
                              @ApiParam(name = "patientId",value = "就诊人id",required = true)
                              @PathVariable Long patientId){
        return Result.ok(orderService.saveOrder(scheduleId,patientId));
    }

    //根据订单id查询订单详情
    @GetMapping("auth/getOrders/{orderId}")
    public Result getOrders(@PathVariable String orderId){
        OrderInfo orderInfo = orderService.getOrder(orderId);
        return Result.ok(orderInfo);
    }

    //订单列表（条件查询带分页）
    @GetMapping("auth/{page}/{limit}")
    public Result list(@PathVariable Long page,@PathVariable Long limit,
                       OrderQueryVo orderQueryVo,HttpServletRequest request){
        //设置当前用户id
        Long userId = AuthContextHolder.getUserId(request);
        orderQueryVo.setUserId(userId);
        Page<OrderInfo> pageParam = new Page<>(page, limit);
        IPage<OrderInfo> pageModel = orderService.selectPage(pageParam, orderQueryVo,userId);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "获取订单状态")
    @GetMapping("auth/getStatusList")
    public Result getStatusList(){
        return Result.ok(OrderStatusEnum.getStatusList());
    }

    @ApiOperation(value = "取消预约")
    @GetMapping("auth/cancelOrder/{orderId}")
    public Result cancelOrder(@PathVariable("orderId") Long orderId) {
        Boolean isOrder = orderService.cancelOrder(orderId);
        return Result.ok(isOrder);
    }

    @ApiOperation(value = "获取订单统计数据")
    @PostMapping("inner/getCountMap")
    public Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo) {
        return orderService.getCountMap(orderCountQueryVo);
    }
}
